package com.ateam.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.GroupsBiz;
import com.ateam.biz.UsersBiz;
import com.ateam.pojo.Companies;
import com.ateam.pojo.Groups;
import com.ateam.pojo.Users;
import com.ateam.util.SplitFactory;

/**
 * @author WuYuan 小组
 */
@Controller
public class GroupsController {
	@Resource(name = "usersBiz")
	private UsersBiz usersBiz;

	@Resource(name = "groupsBiz")
	private GroupsBiz groupsBiz;

	// 创建小组(是公司的人就能创建小组并指导负责人等)
	@ResponseBody
	@RequestMapping("creategroup.do")
	public boolean creategroup(Groups group, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				if (0 != groupsBiz.addGroup(group, user.getUserid())) {
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 根据用户id查看用户所在的小组
	@ResponseBody
	@RequestMapping("showngroups.do")
	public List<Groups> showngroups(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				return groupsBiz.getGroupsByUser(user.getUserid());
			} else
				return null;
		} else
			return null;
	}

	// 选择一个小组后，前往小组的具体细节
	@RequestMapping("gotogroupdetail.do")
	public String groupdetail(String groupid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Groups currentGroup = groupsBiz.getGroupDetail(Integer.parseInt(groupid));
				request.setAttribute("currentGroup", currentGroup);
				return "groupdetail";
			} else
				return "index";
		} else
			return "index";
	}

	// 发布任务、活动时，获得小组中所有人
	@ResponseBody
	@RequestMapping("groupgetallusers.do")
	public List<Users> groupgetallusers(String groupid, HttpSession session, HttpServletRequest request) {
		// 后期修改代码，当前用户和公司都用工厂类获得
		if ( session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				List<Users> users = usersBiz.getUsersByGroupid(Integer.parseInt(groupid));
				return users;
			} else
				return null;
		} else
			return null;
	}

	// 获得还没有加入项目的人
	@ResponseBody
	@RequestMapping("intviteworkernotingroup.do")
	public List<Users> intviteworkernotingroup(String groupid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Companies currentCompany = (Companies) session.getAttribute("currentCompany");
				return usersBiz.getUsersByNoGroupid(currentCompany.getCompanyid(), Integer.parseInt(groupid));
			} else
				return null;
		} else
			return null;
	}

	// 邀请公司中还没有进入小组的人来小组
	@ResponseBody
	@RequestMapping("invitegroupworker.do")
	public boolean invitegroupworker(String groupid, String groupinvitedmemberid, HttpSession session,
			HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				int[] memberids = SplitFactory.getIntarray(groupinvitedmemberid);// 切割参与者id
				if (0 != usersBiz.addUserGroup(Integer.parseInt(groupid), memberids)) {
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}
}
